package com.whut.whw.setting;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.whut.whw.R;
import com.whut.whw.base.BaseActivity;
import com.whut.whw.message.MsgRecyclerViewAdapter;
import com.whut.whw.personalinfo.PersonalInfoAdapter;
import com.whut.whw.personalinfo.PersonalInfoModel;
import com.whut.whw.setting.about.AboutActivity;
import com.whut.whw.util.GsonUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * <pre>
 *  @Decription 设置页面
 *  @datetime 2017/06/27 9:03
 *  @author 忘尘无憾
 *  @version
 * </pre>
 */
public class SettingsActivity extends BaseActivity {

    @BindView(R.id.setting_recycler)
    RecyclerView setting_recycler;//recycler view

    @BindView(R.id.toolbar)
    Toolbar toolbar;//toolbar

    @BindView(R.id.back_login_btn)
    Button back_login_btn;//退出登录

    List<BaseItemModel> informModelLists;//列表项listji集合

    SettingAdapter infoAdapter;//设置适配器

    @Override
    public int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setToolbar();//设置tollbar
        informModelLists = new ArrayList<>();
        try {
            JSONArray array = new JSONArray();
            for (int i = 0; i < 1; i++) {
                JSONObject obj = new JSONObject();

                obj.put("leftIcon", R.drawable.ic_friends_24dp);
                obj.put("title", "关于");
                obj.put("rightIcon", R.drawable.ic_arrow_right_24dp);
                array.put(obj);
            }
            informModelLists = GsonUtils.jsonToList(array.toString(), BaseItemModel.class);

        } catch (Exception e) {
            e.printStackTrace();
        }

        //指定布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        setting_recycler.setLayoutManager(layoutManager);
        //适配器设置
        infoAdapter = new SettingAdapter(context, informModelLists);
        setting_recycler.setAdapter(infoAdapter);
    }

    /**
     * 设置toolbar
     */
    private void setToolbar() {
        toolbar.setTitle(R.string.setting);
        toolbar.setNavigationIcon(R.drawable.rc_back_icon);
    }

    @Override
    public void setListener() {
        //适配器点击项监听
        infoAdapter.setOnItemClickListener(new SettingAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, int position) {
                TextView item_title_tv = (TextView) view.findViewById(R.id.item_title_tv);
                getUiDelegate().toastLong(item_title_tv.getText().toString());
                setTitleClickListener(item_title_tv.getText().toString());
            }

            @Override
            public void OnItemLongClick(View view, int position) {
                getUiDelegate().toastLong(position + "??");
            }
        });

        back_login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUiDelegate().toastLong("退出登录");
                SharedPreferences sp=getSharedPreferences("userinfo",MODE_PRIVATE);
                SharedPreferences.Editor ed=sp.edit();
                ed.clear();
                ed.commit();
            }
        });

        //返回
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setTitleClickListener(String title) {
        if ("关于".equals(title)) {
            startActivity(new Intent(this, AboutActivity.class));
        }
    }
}
